package cn.gzmli.dtt.common.workflow.controller;

import cn.gzmli.dtt.common.annotation.Comment;
import cn.gzmli.dtt.common.annotation.Ignore;
import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.entity.Result;
import cn.gzmli.dtt.common.workflow.entity.FlowNode;
import cn.gzmli.dtt.common.workflow.service.FlowNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping(value="/system")
public class FlowNodeMgr{

	@Autowired
	private FlowNodeService flowNodeService;

	// 分页查询
	@RequestMapping(value = "/flowNodeForGrid")
	@ResponseBody
	@Ignore
	public Map<String, Object> listFlowNodesForGrid(
			HttpServletRequest request,
            HttpServletResponse response,
			PageInfo pageInfo,
			String workFlowID,
			String name,
			Boolean isBind)
			throws Exception {

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		if (workFlowID != null && workFlowID.trim().length()>0) {
			condition.put("workFlowID", "='" + workFlowID + "'");
		}
		if (name != null && name.trim().length()>0) {
			condition.put("name", "LIKE '%" + name + "%'");
		}
		result.put("flowNode",flowNodeService.queryFlowNodesByPage(condition, pageInfo));
		result.put("pageInfo", pageInfo);
		return result;
	}

	// 分页查询
	@RequestMapping(value = "/flowNodeForUserMessageGrid")
	@ResponseBody
	@Ignore
	public Map<String, Object> listFlowNodesForUserMessageGrid(HttpServletRequest request,
                                                               HttpServletResponse response, PageInfo pageInfo, String workFlowName,
                                                               String workFlowID, String name, Integer userID, Integer status, Boolean isBind)
			throws Exception {

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		if (workFlowName != null && workFlowName.trim().length()>0) {
			condition.put(" AND t.workFlowName ", "LIKE '%" + workFlowName + "%'");
		}
		if (workFlowID != null && workFlowID.trim().length()>0) {
			condition.put(" AND t.workFlowID ", "='" + workFlowID + "'");
		}
		if (name != null && name.trim().length()>0) {
			condition.put(" AND t.name ", " LIKE '%" + name + "%'");
		}
		if(status != null){
			condition.put(" AND t.status ", "=" + status);
		}
		if(isBind == null){
			isBind = true;
		}
		result.put("flowNode",flowNodeService.queryFlowNodeForUserMessage(condition, pageInfo, isBind, userID));
		result.put("pageInfo", pageInfo);
		return result;
	}

	// 添加流程节点
	@RequestMapping(value = "/addFlowNode")
	@ResponseBody
	@Comment("流程节点添加")
	public Map<String, Object> addFlowNode(
			HttpServletRequest request,
            HttpServletResponse response,
			FlowNode flowNode
	) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		flowNodeService.addFlowNode(flowNode);
		result.put("state", 1);
		result.put("msg", "流程节点添加成功！");
		return result;
	}

	// 修改流程节点
	@RequestMapping(value = "/changeFlowNode")
	@ResponseBody
	@Comment("流程节点修改")
	public Map<String, Object> changeFlowNode(
			HttpServletRequest request,
            HttpServletResponse response,
			FlowNode flowNode
	) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		flowNodeService.changeFlowNode(flowNode);
		result.put("state", 1);
		result.put("msg", "流程节点修改成功！");
		return result;
	}

	// 删除流程节点
	@RequestMapping(value = "/deleteFlowNode")
	@ResponseBody
	@Comment("流程节点删除")
	public Map<String, Object> deleteFlowNode(
			HttpServletRequest request,
            HttpServletResponse response,
			FlowNode flowNode
	) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		flowNodeService.deleteFlowNode(flowNode);
		result.put("state", 1);
		result.put("msg", "流程节点删除成功！");
		return result;
	}

	// 获取流程节点下拉列表
	@RequestMapping(value = "/listTree")
	@ResponseBody
	@Ignore
	public Result listTree() throws Exception {
		return new Result(1, "获取流程节点成功", this.flowNodeService.getTree());
	}
}
